{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 字典\n",
    "- 使用字典\n",
    "- 访问字典中的值\n",
    "- 添加键值对"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Kobe\n",
      "24\n",
      "{'name': 'Kobe', 'number': 24}\n"
     ]
    }
   ],
   "source": [
    "# 字典用{}中放键值对表示\n",
    "player = {'name':'Kobe','number':24}\n",
    "# 访问\n",
    "print(player['name'])\n",
    "print(player['number'])\n",
    "print(player)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'Kobe', 'number': 24, 'city': 'LA'}\n"
     ]
    }
   ],
   "source": [
    "# 添加键值对\n",
    "player['city'] = 'LA'\n",
    "print(player)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 从空字典添加键值对\n",
    "player = {}\n",
    "player"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'Kobe', 'number': 24, 'city': 'LA'}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "player['name'] = 'Kobe'\n",
    "player['number'] = 24\n",
    "player['city'] = 'LA'\n",
    "player"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Original number: 24\n",
      "new number: 8\n"
     ]
    }
   ],
   "source": [
    "# 修改元素的值\n",
    "print(\"Original number:\",player['number'])\n",
    "player['number'] = 8\n",
    "print(\"new number:\",player['number'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'Kobe', 'number': 8}\n",
      "{'name': 'Kobe'}\n"
     ]
    }
   ],
   "source": [
    "print(player)\n",
    "# 删除键值对\n",
    "del player['number']\n",
    "print(player)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_items([('username', 'lauuu'), ('first', 'Lau'), ('last', 'Phil')])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 遍历字典\n",
    "user_0 = {\n",
    "    'username' : 'lauuu',\n",
    "    'first':'Lau',\n",
    "    'last': 'Phil',\n",
    "}\n",
    "user_0.items()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Key:username\n",
      "\n",
      "Value:lauuu\n",
      "\n",
      "Key:first\n",
      "\n",
      "Value:Lau\n",
      "\n",
      "Key:last\n",
      "\n",
      "Value:Phil\n"
     ]
    }
   ],
   "source": [
    " for key,value in user_0.items():\n",
    "    print('\\nKey:'+key)\n",
    "    print('\\nValue:'+value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "username\n",
      "first\n",
      "last\n"
     ]
    }
   ],
   "source": [
    "# 遍历所有的键\n",
    "for key in user_0.keys():\n",
    "    print(key)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "first\n",
      "last\n",
      "username\n"
     ]
    }
   ],
   "source": [
    "# 按顺序遍历所有的键\n",
    "for key in sorted(user_0.keys()):\n",
    "    print(key)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "lauuu\n",
      "Lau\n",
      "Phil\n"
     ]
    }
   ],
   "source": [
    "# 遍历所有的值\n",
    "for value in user_0.values():\n",
    "    print(value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'number': 1, 'points': 10}\n",
      "{'number': 2, 'points': 2}\n",
      "{'number': 8, 'points': 81}\n"
     ]
    }
   ],
   "source": [
    " # 字典列表\n",
    "player_01 = {'number':1,'points':10}\n",
    "player_02 = {'number':2,'points':2}\n",
    "player_03 = {'number':8,'points':81}\n",
    "\n",
    "# 组成列表\n",
    "players = [player_01,player_02,player_03]\n",
    "\n",
    "for player in players:\n",
    "    print(player)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "You ordered a thick-crust pizzawith the following toppings:\n",
      "\tmushrooms\n",
      "\textra cheese\n"
     ]
    }
   ],
   "source": [
    "# 在字典中储存列表\n",
    "pizza = {\n",
    "    'crust':'thick',\n",
    "    'toppings':['mushrooms','extra cheese'],\n",
    "}\n",
    "\n",
    "print('You ordered a ' + pizza['crust'] + '-crust pizza' + 'with the following toppings:')\n",
    "for topping in pizza['toppings']:\n",
    "    print('\\t' + topping)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Jen's favorite language are:'\n",
      "\tPython\n",
      "\tRuby\n",
      "\n",
      "Sarah's favorite language are:'\n",
      "\tC\n",
      "\n",
      "Edward's favorite language are:'\n",
      "\tRuby\n",
      "\tGo\n",
      "\n",
      "Phil's favorite language are:'\n",
      "\tPython\n",
      "\tHaskell\n"
     ]
    }
   ],
   "source": [
    "favorite_languages = {\n",
    "    'jen':['python','ruby'],\n",
    "    'sarah':['c'],\n",
    "    'edward':['ruby','go'],\n",
    "    'phil':['python','haskell'],\n",
    "}\n",
    "for name , languages in favorite_languages.items():\n",
    "    print('\\n' + name.title() + \"'s favorite language are:'\")\n",
    "    for language in languages:\n",
    "        print(\"\\t\" + language.title())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Username:aeinstein\n",
      "\tFull name:Albert Einstein\n",
      "\tLocation:Princeton\n",
      "\n",
      "Username:mcurie\n",
      "\tFull name:Marie Curie\n",
      "\tLocation:Paris\n"
     ]
    }
   ],
   "source": [
    "# 在字典里储存字典\n",
    "users = {\n",
    "    'aeinstein':{\n",
    "        'first':'albert',\n",
    "        'last':'einstein',\n",
    "        'location':'princeton',\n",
    "    },\n",
    "    'mcurie':{\n",
    "        'first':'marie',\n",
    "        'last':'curie',\n",
    "        'location':'paris',\n",
    "    },\n",
    "}\n",
    "\n",
    "for username , user_info in users.items():\n",
    "    print(\"\\nUsername:\" + username)\n",
    "    full_name = user_info['first'] + \" \" + user_info['last']\n",
    "    location = user_info['location']\n",
    "    \n",
    "    print('\\tFull name:' + full_name.title())\n",
    "    print('\\tLocation:' + location.title())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
